/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *   gbx-emu - settingsdialog.cpp                                          *
 *   gbx-emu homepage: http://code.google.com/p/gbx-emu/                   *
 *   Copyright (C) 2009 olejl77@gmail.com                                  *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "settingsdialog.h"
#include "getinput.h"
#include "ui_settingsdialog.h"
#include "../core/common.h"
#include <QSettings>

SettingsDialog::SettingsDialog(QWidget *parent, QSettings *ptr, Log *ptr2) :
    QDialog(parent),
    m_ui(new Ui::SettingsDialog)
{
    m_settings = ptr;
    m_log = ptr2;
    m_ui->setupUi(this);

    connect(m_ui->checkDebugger,     SIGNAL(toggled(bool)), this, SLOT(enableDebugger(bool)));
    connect(m_ui->checkFrameLimiter, SIGNAL(toggled(bool)), this, SLOT(enableFrameLimiter(bool)));

    smLog = new QSignalMapper(this);
    connect(m_ui->checkCart,         SIGNAL(toggled(bool)), smLog, SLOT(map()));
    connect(m_ui->checkCpu,          SIGNAL(toggled(bool)), smLog, SLOT(map()));
    connect(m_ui->checkDebugger_2,   SIGNAL(toggled(bool)), smLog, SLOT(map()));
    connect(m_ui->checkGameboy,      SIGNAL(toggled(bool)), smLog, SLOT(map()));
    connect(m_ui->checkGui,          SIGNAL(toggled(bool)), smLog, SLOT(map()));
    connect(m_ui->checkMain,         SIGNAL(toggled(bool)), smLog, SLOT(map()));
    connect(m_ui->checkMemory,       SIGNAL(toggled(bool)), smLog, SLOT(map()));
    connect(m_ui->checkScreen,       SIGNAL(toggled(bool)), smLog, SLOT(map()));
    smLog->setMapping(m_ui->checkCart,       LOG_CART);
    smLog->setMapping(m_ui->checkCpu,        LOG_CPU);
    smLog->setMapping(m_ui->checkDebugger_2, LOG_DEBUGGER);
    smLog->setMapping(m_ui->checkGameboy,    LOG_GAMEBOY);
    smLog->setMapping(m_ui->checkGui,        LOG_GUI);
    smLog->setMapping(m_ui->checkMain,       LOG_MAIN);
    smLog->setMapping(m_ui->checkMemory,     LOG_MEMORY);
    smLog->setMapping(m_ui->checkScreen,     LOG_SCREEN);
    connect(smLog, SIGNAL(mapped(const int)), this, SLOT(clicked(const int)));

    smLevel = new QSignalMapper(this);
    connect(m_ui->checkError,   SIGNAL(toggled(bool)), smLevel, SLOT(map()));
    connect(m_ui->checkWarning, SIGNAL(toggled(bool)), smLevel, SLOT(map()));
    connect(m_ui->checkInfo,    SIGNAL(toggled(bool)), smLevel, SLOT(map()));
    connect(m_ui->checkDebug,   SIGNAL(toggled(bool)), smLevel, SLOT(map()));
    connect(m_ui->checkLog,     SIGNAL(toggled(bool)), smLevel, SLOT(map()));
    smLevel->setMapping(m_ui->checkError, ERROR_LEVEL);
    smLevel->setMapping(m_ui->checkWarning, WARNING_LEVEL);
    smLevel->setMapping(m_ui->checkInfo, INFO_LEVEL);
    smLevel->setMapping(m_ui->checkDebug, DEBUG_LEVEL);
    smLevel->setMapping(m_ui->checkLog, LOG_LEVEL);
    connect(smLevel, SIGNAL(mapped(const int)), this, SLOT(clickedLevel(const int)));

    smKey = new QSignalMapper(this);
    connect(m_ui->pushUp,    SIGNAL(clicked()), smKey, SLOT(map()));
    connect(m_ui->pushDown,  SIGNAL(clicked()), smKey, SLOT(map()));
    connect(m_ui->pushLeft,  SIGNAL(clicked()), smKey, SLOT(map()));
    connect(m_ui->pushRight, SIGNAL(clicked()), smKey, SLOT(map()));
    smKey->setMapping(m_ui->pushUp,    KeyUp);
    smKey->setMapping(m_ui->pushDown,  KeyDown);
    smKey->setMapping(m_ui->pushLeft,  KeyLeft);
    smKey->setMapping(m_ui->pushRight, KeyRight);
    connect(smKey, SIGNAL(mapped(const int)), this, SLOT(clickedKey(const int)));

    readSettings();
}

SettingsDialog::~SettingsDialog()
{
    delete m_ui;
}

void SettingsDialog::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void SettingsDialog::readSettings()
{
    bool checked;
    s32 mask;

    m_ui->checkDebugger->setChecked(m_settings->value("EmulatorSettings/Debugger", false).toBool());
    m_ui->checkFrameLimiter->setChecked(m_settings->value("EmulatorSettings/FrameLimiter", true).toBool());

    mask = m_log->getLogMask();
    checked = (mask & LOG_CART) ? true : false;
    m_ui->checkCart->setChecked(checked);
    checked = (mask & LOG_CPU) ? true : false;
    m_ui->checkCpu->setChecked(checked);
    checked = (mask & LOG_DEBUGGER) ? true : false;
    m_ui->checkDebugger_2->setChecked(checked);
    checked = (mask & LOG_GAMEBOY) ? true : false;
    m_ui->checkGameboy->setChecked(checked);
    checked = (mask & LOG_GUI) ? true : false;
    m_ui->checkGui->setChecked(checked);
    checked = (mask & LOG_MAIN) ? true : false;
    m_ui->checkMain->setChecked(checked);
    checked = (mask & LOG_MEMORY) ? true : false;
    m_ui->checkMemory->setChecked(checked);
    checked = (mask & LOG_SCREEN) ? true : false;
    m_ui->checkScreen->setChecked(checked);

    mask = m_log->getLogLevel();
    checked = (mask & ERROR_LEVEL) ? true : false;
    m_ui->checkError->setChecked(checked);
    checked = (mask & WARNING_LEVEL) ? true : false;
    m_ui->checkWarning->setChecked(checked);
    checked = (mask & INFO_LEVEL) ? true : false;
    m_ui->checkInfo->setChecked(checked);
    checked = (mask & DEBUG_LEVEL) ? true : false;
    m_ui->checkDebug->setChecked(checked);
    checked = (mask & LOG_LEVEL) ? true : false;
    m_ui->checkLog->setChecked(checked);
}

void SettingsDialog::clickedLevel(int value)
{
    bool checked = false;
    switch (value) {
        case (ERROR_LEVEL):
            checked = m_ui->checkError->isChecked();
            break;
        case (WARNING_LEVEL):
            checked = m_ui->checkWarning->isChecked();
            break;
        case (INFO_LEVEL):
            checked = m_ui->checkInfo->isChecked();
            break;
        case (DEBUG_LEVEL):
            checked = m_ui->checkDebug->isChecked();
            break;
        case (LOG_LEVEL):
            checked = m_ui->checkLog->isChecked();
            break;
    }
    if (checked) {
        m_log->enableLevel(value);
    } else {
        m_log->disableLevel(value);
    }
}

void SettingsDialog::clicked(int value)
{
    bool checked = false;
    switch (value) {
        case (LOG_CART):
            checked = m_ui->checkCart->isChecked();
            break;
        case (LOG_CPU):
            checked = m_ui->checkCpu->isChecked();
            break;
        case (LOG_DEBUGGER):
            checked = m_ui->checkDebugger_2->isChecked();
            break;
        case (LOG_GAMEBOY):
            checked = m_ui->checkGameboy->isChecked();
            break;
        case (LOG_GUI):
            checked = m_ui->checkGui->isChecked();
            break;
        case (LOG_MAIN):
            checked = m_ui->checkMain->isChecked();
            break;
        case (LOG_MEMORY):
            checked = m_ui->checkMemory->isChecked();
            break;
        case (LOG_SCREEN):
            checked = m_ui->checkScreen->isChecked();
            break;
    }
    if (checked) {
        m_log->enableType(value);
    } else {
        m_log->disableType(value);
    }
}

void SettingsDialog::enableDebugger(bool enabled)
{
    m_settings->setValue("EmulatorSettings/Debugger",enabled);
}

void SettingsDialog::enableFrameLimiter(bool enabled)
{
    m_settings->setValue("EmulatorSettings/FrameLimiter",enabled);
}

void SettingsDialog::clickedKey(int value)
{
    int key;
    GetInput d(this, &key);
    d.exec();

    switch (value) {
        case (KeyUp):
            m_settings->setValue("KeyBindings/Up", key);
            break;
        case (KeyDown):
            m_settings->setValue("KeyBindings/Down", key);
            break;
        case (KeyLeft):
            m_settings->setValue("KeyBindings/Left", key);
            break;
        case (KeyRight):
            m_settings->setValue("KeyBindings/Right", key);
            break;
        case (KeySelect):
            m_settings->setValue("KeyBindings/Select", key);
            break;
        case (KeyStart):
            m_settings->setValue("KeyBindings/Start", key);
            break;
        case (KeyA):
            m_settings->setValue("KeyBindings/A", key);
            break;
        case (KeyB):
            m_settings->setValue("KeyBindings/B", key);
            break;
    }
}
